

package com.songbl.bless.app.controller;



import com.songbl.bless.app.entity.ContactsEntity;
import com.songbl.bless.app.entity.UserAndGroupEntity;
import com.songbl.bless.app.service.ContactsService;
import com.songbl.bless.common.utils.R;
import com.songbl.bless.app.entity.ContactsGroupEntity;
import com.songbl.bless.app.form.ContractsGroupForm;
import com.songbl.bless.app.service.ContactsGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 测试使用：Postman
 *   URL： http://localhost:8080/rest-boot/app/login
 *   Header：content-type：application/json;chartset=utf-8
 *   Body: {
 * 	          "mobile": "13336371247",
 *             "password": "123456"
 *         }
 *
 * APP登录授权
 *
 * 791243928@qq.com
 * 1.@RestController注解：由下面两个注解构成
 *      @Controller
 *      @ResponseBody  //自动将数据（map或者对象）转换为JSON格式
 * 2. @RequestBody ：  将请求的json转化为对象 （通过jackson）
 *
 */
@RestController
@RequestMapping("/app")
//@Api("APP登录接口")
public class ContactsController {
    private static Logger logger = LoggerFactory.getLogger(ContactsController.class);

    @Autowired(required = false)
    private ContactsGroupService contactsGroupService;

    @Autowired(required = false)
    private ContactsService contactsService ;


    /**
     * 获取用户下所有联系人组
     */
    @PostMapping("contactsGroup")
    public R getContactsGroup(@RequestBody ContractsGroupForm form){
        System.out.println("联系人 "+form.toString());

        //用户登录
        List<ContactsGroupEntity> contactsGroup = contactsGroupService.getContactsGroup(form);

        Map<String, Object> map = new HashMap<>();
        map.put("result",contactsGroup);
        return R.ok(map);
    }
    /**
     *  查询 用户、所有组新
     * @param form
     * @return
     */

    @PostMapping("queryUserAndListById")
    public R queryUserAndListById(@RequestBody ContactsGroupEntity form){
        System.out.println("联系人 "+form.toString());
        List<UserAndGroupEntity> contactsGroupEntity = contactsGroupService.queryUserAndListById(form.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("result",contactsGroupEntity);
        return R.ok(map);
    }


    /**
     *  查询 用户 、组 联系人
     * @param form
     * @return
     */
    @PostMapping("queryUserAndGroupAndContactsById")
    public R queryUserAndGroupAndContactsById(@RequestBody ContactsGroupEntity form){
        System.out.println("联系人 "+form.toString());
        List<UserAndGroupEntity> contactsGroupEntity = contactsGroupService.queryUserAndGroupAndContactsById(form.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("result",contactsGroupEntity);
        return R.ok(map);
    }
    /**
     * 添加联系人
     * {
     * 	"groupName": "赵德华的朋友",
     * 	"userId": 7
     * }
     *
     * {
     *     "msg": "success",
     *     "result": true,
     *     "code": 0
     * }
     */
    @PostMapping("addContactsGroup")
    public R addContractGroup(@RequestBody ContactsGroupEntity form){
        System.out.println("组 "+form.toString());
        Boolean aBoolean = contactsGroupService.addContractGroup(form);
        Map<String, Object> map = new HashMap<>();
        map.put("result",aBoolean);
        return R.ok(map);
    }

    /**
     *  删除联系人组
     * @param form
     * @return
     */

    @PostMapping("deleteContactsGroup")
    public R deleteContactsGroup(@RequestBody ContactsGroupEntity form){
        System.out.println("组 "+form.toString());
        Boolean aBoolean = contactsGroupService.deleteContractGroup(form);
        Map<String, Object> map = new HashMap<>();
        map.put("result",aBoolean);
        return R.ok(map);
    }

    /**
     *  修改组
     * @param form
     * @return
     */
    @PostMapping("updateContactsGroup")
    public R upDateContactsGroup(@RequestBody ContactsGroupEntity form){
        System.out.println("组 "+form.toString());
        Boolean aBoolean = contactsGroupService.updateContractGroup(form);
        Map<String, Object> map = new HashMap<>();
        map.put("result",aBoolean);
        return R.ok(map);
    }

    /**
     *  添加联系人
     *
     */
    @PostMapping("addContacts")
    public R addContacts(@RequestBody ContactsEntity contactsEntity){
        System.out.println("联系人 "+contactsEntity.toString());
        Boolean aBoolean = contactsService.addContract(contactsEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("result",aBoolean);
        return R.ok(map);
    }
    /**
     *  添加联系人
     *
     */
    @PostMapping("deleteContacts")
    public R deleteContacts(@RequestBody ContactsEntity contactsEntity){
        System.out.println("联系人 "+contactsEntity.toString());
        Boolean aBoolean = contactsService.deleteContract(contactsEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("result",aBoolean);
        return R.ok(map);
    }

    /**
     *  添加联系人
     *
     */
    @PostMapping("updateContacts")
    public R updateContacts(@RequestBody ContactsEntity contactsEntity){
        System.out.println("联系人 "+contactsEntity.toString());
        Boolean aBoolean = contactsService.updateContact(contactsEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("result",aBoolean);
        return R.ok(map);
    }

}
